package cn.jekun.shop.app.action;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.common.service.ForgetPwdService;

/**
 * 忘记密码
 * @author twj
 *
 */
@SuppressWarnings("serial")
public class ForgetPwdAction extends AppBaseAction{
	@Resource(name = ForgetPwdService.BEAN_NAME)
	ForgetPwdService forgetPwdService;
	/**
	 * 手机是否注册，验证码
	 * @return
	 */
	public String getForgetPwdValidateCode(){
		JSONObject jsonObject = null;
		try {
			String mobile = this.request.getParameter("mobile");
			this.validateSign(mobile);
			jsonObject = forgetPwdService.getForgetPwdValidateCode(mobile);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	public String validateMessageCode(){
		JSONObject jsonObject = null;
		try {
			String mobile = this.request.getParameter("mobile");
			String validateCode = this.request.getParameter("validateCode");
			this.validateSign(mobile,validateCode);
			jsonObject = forgetPwdService.checkValidateCode(mobile,validateCode);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 重置密码
	 * @return
	 */
	public String resetLoginPwd(){
		JSONObject jsonObject = null;
		try {
			String mobile = this.request.getParameter("mobile");
			String newPwd = this.request.getParameter("newPwd");
			this.validateSign(mobile,newPwd);
			jsonObject = forgetPwdService.resetLoginPwd(mobile,newPwd);
			
			this.logService.addMallLog(this.getUserId(),this.getBrowser(), this.getIp(), "手机重置密码");//记录操作日志
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
}
